package kamkor.webtools.url

/** 
 * Parses urls from text
 * @author kamkor
 *
 */
object UrlParser {
	
	/**
	 * Parses urls from text. Urls can be in img forum tags or img markup. 
	 * 
	 * @param text with urls
	 * @return Array[String] of urls
	 */
	def parseUrlPathAndFileIncluded(text: String): Array[String] = {
		val regex = UrlPatterns.hostAndPathAndFilePattern.r
		(for (url <- regex.findAllIn(text)) yield url).toArray
	}
	
	/**
	 * Parses urls from text. Urls can be in img forum tags or img markup.
	 * 
	 * Urls must end with specified extensions, for example Array("zip", "rar")
	 * 
	 * @param text with urls
	 * @return Array[String] of urls	
	 */
	def parseUrlPathAndFileIncluded(text: String, extensions: Array[String]): Array[String] = {
		val regex = UrlPatterns.hostAndPathAndFilePattern.r		
		val urls = for {
			url <- regex.findAllIn(text)
			ext <- extensions.elements
			if url.matches("""(?i).*"""+ ext +"""$""")
		} yield url		
		urls.toArray
	}
}